<template>
  <el-card>
    <el-button size="small" type="primary" @click="handleAdd()">
      新增学习路径
    </el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="路径名称" />
      <el-table-column prop="description" label="路径描述" />
      <el-table-column prop="difficulty" label="难度" width="100" />
      <el-table-column prop="days" label="总天数" width="80" />
      <el-table-column prop="user_id" label="用户ID" width="100" />
      <el-table-column prop="is_recommended" label="是否推荐" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.is_recommended === 1 ? 'success' : 'info'">
            {{ scope.row.is_recommended === 1 ? '推荐' : '不推荐' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="handleView(scope.row)">
            查看
          </el-button>
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">
            编辑
          </el-button>
          <el-popconfirm title="您确定要删除吗?" @confirm="handleRemove(scope.row.id)">
            <template #reference>
              <el-button size="small" type="warning">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查看对话框 -->
    <el-dialog v-model="viewDialogVisible" title="路径详情" width="50%">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="ID">{{ currentPath.id }}</el-descriptions-item>
        <el-descriptions-item label="路径名称">{{ currentPath.name }}</el-descriptions-item>
        <el-descriptions-item label="路径描述">{{ currentPath.description }}</el-descriptions-item>
        <el-descriptions-item label="难度">{{ currentPath.difficulty }}</el-descriptions-item>
        <el-descriptions-item label="总天数">{{ currentPath.days }}</el-descriptions-item>
        <el-descriptions-item label="包含任务">{{ currentPath.tasks }}</el-descriptions-item>
        <el-descriptions-item label="缩略图">
          <img v-if="currentPath.thumbnail_url" :src="currentPath.thumbnail_url" style="max-width: 200px" />
          <span v-else>无图片</span>
        </el-descriptions-item>
        <el-descriptions-item label="用户ID">{{ currentPath.user_id }}</el-descriptions-item>
        <el-descriptions-item label="排序">{{ currentPath.sort }}</el-descriptions-item>
        <el-descriptions-item label="是否推荐">{{ currentPath.is_recommended === 1 ? '推荐' : '不推荐' }}</el-descriptions-item>
        <el-descriptions-item label="用户笔记">{{ currentPath.user_note || '无' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ currentPath.created_at }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ currentPath.updated_at }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 编辑对话框 -->
    <el-dialog v-model="editDialogVisible" :title="editDialogTitle" width="50%">
      <el-form :model="currentPath" label-width="80px">
        <el-form-item label="ID">
          <el-input v-model="currentPath.id" disabled />
        </el-form-item>
        <el-form-item label="路径名称" required>
          <el-input v-model="currentPath.name" />
        </el-form-item>
        <el-form-item label="路径描述">
          <el-input v-model="currentPath.description" type="textarea" />
        </el-form-item>
        <el-form-item label="难度" required>
          <el-input v-model="currentPath.difficulty" />
        </el-form-item>
        <el-form-item label="总天数" required>
          <el-input v-model="currentPath.days" type="number" />
        </el-form-item>
        <el-form-item label="包含任务">
          <el-input v-model="currentPath.tasks" type="textarea" />
        </el-form-item>
        <el-form-item label="缩略图URL">
          <el-input v-model="currentPath.thumbnail_url" />
        </el-form-item>
        <el-form-item label="用户ID" required>
          <el-input v-model="currentPath.user_id" type="number" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="currentPath.sort" type="number" />
        </el-form-item>
        <el-form-item label="是否推荐">
          <el-select v-model="currentPath.is_recommended" placeholder="请选择">
            <el-option label="推荐" value="1" />
            <el-option label="不推荐" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户笔记">
          <el-input v-model="currentPath.user_note" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axiosObject from '@/main.js';

const tableData = ref([])
const viewDialogVisible = ref(false)
const editDialogTitle = ref("")
const editDialogVisible = ref(false)
const currentPath = ref({})

const handleView = (row) => {
  currentPath.value = { ...row }
  viewDialogVisible.value = true
}

const handleAdd = () => {
  editDialogTitle.value = "添加学习路径";
  currentPath.value = {
    days: 1,
    sort: 0,
    is_recommended: 0,
    user_id: 0
  };
  editDialogVisible.value = true;
}

const handleEdit = (row) => {
  editDialogTitle.value = "编辑学习路径";
  currentPath.value = { ...row }
  editDialogVisible.value = true
}

const handleRemove = async (id) => {
  try {
    await axiosObject.delete(`learningPath/${id}`);
    await findPathList();
  } catch (error) {
    console.error("删除失败:", error);
  }
}

const handleSave = async () => {
  try {
    if (currentPath.value.id) {
      // 修改路径
      await axiosObject.put(`learningPath/${currentPath.value.id}`, currentPath.value);
    } else {
      // 新增路径
      await axiosObject.post("learningPath", currentPath.value);
    }
    await findPathList();
    editDialogVisible.value = false
  } catch (error) {
    console.error("保存失败:", error);
  }
}

const findPathList = async () => {
  try {
    const result = await axiosObject.get("learningPath");
    console.log("学习路径数据:", result.data.resultData);
    tableData.value = result.data.resultData || [];
  } catch (error) {
    console.error("获取学习路径列表失败:", error);
    tableData.value = [];
  }
}

onMounted(() => {
  console.log("组件挂载完成，开始加载学习路径列表");
  findPathList();
});
</script>